using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Omniture.Studio.Objects;

namespace Omniture.Studio.UserControls
{
  public partial class ErrorControl : ListView
  {
    public ErrorControl()
    {
      InitializeComponent();

      //System.Type[] typeList = new System.Type[] { typeof(MessageTypeConstants), typeof(int), typeof(string), typeof(string) };
      //this.ListViewItemSorter = new ListViewComparer(this, typeList);

      this.Columns.Clear();
      this.Columns.Add("", 24, HorizontalAlignment.Left);
      this.Columns.Add("", 24, HorizontalAlignment.Right);
      this.Columns.Add("Description", 400, HorizontalAlignment.Left);

      this.Sorting = SortOrder.Ascending;
      this.SmallImageList = this._imageList;
      this.View = View.Details;
      this.HideSelection = false;
      this.FullRowSelect = true;
    }

    #region Class Members

    protected Image _iconError = null;
    protected Image _iconWarning = null;
		protected Image _iconText = null;
		protected Omniture.Studio.Objects.Message _message = null;
    private ImageList _imageList = new ImageList();

    #endregion

    #region Property Implementations

		[Browsable(true)]
		[Description("The icon to use for error messages.")]
		public Image IconError
    {
      get { return _iconError; }
      set
      {
        _iconError = value;
        this.SetupImageList();
      }
    }

		[Browsable(true)]
		[Description("The icon to use for warning messages.")]
    public Image IconWarning
    {
      get { return _iconWarning; }
      set
      {
        _iconWarning = value;
        this.SetupImageList();
      }
    }

		[Browsable(true)]
		[Description("The icon to use for information messages.")]
		public Image IconText
		{
			get { return _iconText; }
			set
			{
				_iconText = value;
				this.SetupImageList();
			}
		}

		[Browsable(false)]
		public Omniture.Studio.Objects.Message SelectedMessage
    {
      get { return _message; }
      set { _message = value; }
    }

    #endregion

    #region Methods

    private void SetupImageList()
    {
      _imageList.Images.Clear();

			//Text Icon 1
			if (this.IconText == null)
				_imageList.Images.Add(this.GetIcon("Default.ico"));
			else
				_imageList.Images.Add(this.IconText);

			//Warning Icon 2
			if (this.IconWarning == null)
        _imageList.Images.Add(this.GetIcon("Default.ico"));
      else
        _imageList.Images.Add(this.IconWarning);

			//Error Icon 3
			if (this.IconError == null)
				_imageList.Images.Add(this.GetIcon("Default.ico"));
			else
				_imageList.Images.Add(this.IconError);

    }

    public void ClearMessages()
    {
      this.Items.Clear();
    }
		
		public void AddMessages(List<Omniture.Studio.Objects.Message> messageCollection)
    {
			foreach (Omniture.Studio.Objects.Message message in messageCollection)
      {
        this.AddMessage(message);
      }
    }

    public void AddMessage(Omniture.Studio.Objects.Message message)
    {
      ListViewItem newItem = new ListViewItem();
      newItem.Tag = message;

      //Image
			if (message.MessageType == MessageTypeConstants.Info) newItem.ImageIndex = 0;
			else if (message.MessageType == MessageTypeConstants.Warning) newItem.ImageIndex = 1;
			else if (message.MessageType == MessageTypeConstants.Error) newItem.ImageIndex = 2;

      //Error Number
			newItem.SubItems.Add((this.Items.Count + 1).ToString());

      //Message
      newItem.SubItems.Add(message.Text);

      //Add to the list
      this.Items.Add(newItem);
    }

    private Icon GetIcon(string iconName)
    {
      return new Icon(GetProjectFileAsStream(iconName));
    }

    private System.IO.Stream GetProjectFileAsStream(string iconName)
    {
      try
      {
        System.Reflection.Assembly asbly = System.Reflection.Assembly.GetExecutingAssembly();
				System.IO.Stream stream = asbly.GetManifestResourceStream("Omniture.Studio.images." + iconName);
        System.IO.StreamReader sr = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8);
        return sr.BaseStream;
      }
      catch (Exception ex)
      {
        throw;
      }
    }

    #endregion

  }
}